<template>
  <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible" :close-on-click-modal="false">
    <el-form ref="dataForm" :rules="rules" :model="temp" label-position="left" label-width="80px" style="width: 80%; margin-left:50px;">
      <el-row :gutter="40">
        <el-col :span="12">
          <el-form-item label="描述" prop="jobDesc">
            <el-input v-model="temp.jobDesc" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="flinkx运行脚本" prop="jobJson">
            <el-input v-model="temp.jobJson" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="jvm参数" prop="jvmParam">
            <el-input v-model="temp.jvmParam" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="增量初始时间" prop="incStartTime">
            <el-input v-model="temp.incStartTime" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="增量时间格式" prop="replaceParamType">
            <el-input v-model="temp.replaceParamType" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="reader表名称" prop="readerTable">
            <el-input v-model="temp.readerTable" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="dialogFormVisible = false">
        {{ $t('table.cancel') }}
      </el-button>
      <el-button type="primary" :loading="loading" @click="dialogStatus==='create'?createData():updateData()">
        {{ $t('table.confirm') }}
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
import { createDataExtractionDetail, updateDataExtractionDetail, getDataExtractionDetail } from '@/api/dataextractiondetail/dataExtractionDetail'

export default {
  name: 'DataExtractionDetailForm',
  data() {
    return {
      rules: {
        jobJson: [{ required: true, message: 'flinkx运行脚本为必填项', trigger: 'blur' }]
                        },
      temp: {},
      textMap: {
        update: '编辑',
        create: '新建'
      },
      loading: false,
      dialogFormVisible: false,
      dialogStatus: ''
    }
  },
  methods: {
    getList() {
      this.$emit('refreshList')
    },
    resetTemp() {
      this.loading = false
      this.temp = {
        id: undefined,
              jobDesc: undefined,
              jobJson: undefined,
              replaceParam: undefined,
              jvmParam: undefined,
              incStartTime: undefined,
              partitionInfo: undefined,
              lastHandleCode: undefined,
              replaceParamType: undefined,
              readerTable: undefined,
              primaryKey: undefined,
              incStartId: undefined,
              incrementType: undefined,
              datasourceId: undefined,
              createBy: undefined,
              createDate: undefined,
              updateDate: undefined,
              updateBy: undefined,
              delFlag: undefined,
              tenantId: undefined,
              remarks: undefined,
              extractionType: undefined,
              dataExtractionId: undefined
            }
    },
    handleCreate() {
      this.resetTemp()
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    createData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.loading = true
          createDataExtractionDetail(this.temp).then(response => {
            this.loading = false
            if (response.data.code === 0) {
              this.getList()
              this.dialogFormVisible = false
              this.$message.success(response.data.msg)
            } else {
              this.$message.error(response.data.msg)
            }
          })
        }
      })
    },
    handleUpdate(id) {
      this.resetTemp()
      this.dialogStatus = 'update'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
      getDataExtractionDetail(id).then(response => {
        if (response.data.code === 0) {
          this.temp = response.data.data
        } else {
          this.dialogFormVisible = false
          this.$message.error(response.data.msg)
        }
      })
    },
    updateData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.loading = true
          updateDataExtractionDetail(this.temp).then(response => {
            this.loading = false
            if (response.data.code === 0) {
              this.dialogFormVisible = false
              this.getList()
              this.$message.success(response.data.msg)
            } else {
              this.$message.error(response.data.msg)
            }
          })
        }
      })
    }
  }
}
</script>